package cn.nawang.ebeim.client.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.bean.BackStrategy;
import cn.nawang.ebeim.client.bean.BackupLog;
import cn.nawang.ebeim.client.bean.Customer;
import cn.nawang.ebeim.client.bean.CustomerEncryption;
import cn.nawang.ebeim.client.bean.DataSource;
import cn.nawang.ebeim.client.bean.FileChangeLog;
import cn.nawang.ebeim.client.bean.HeartLog;
import cn.nawang.ebeim.client.bean.StorageCloud;
import cn.nawang.ebeim.client.bean.StorageFile;
import cn.nawang.ebeim.client.bean.StorageServer;
import cn.nawang.ebeim.client.bean.TbStorageStrategy;
import cn.nawang.ebeim.client.exception.ApiRequestException;

/**
 * @ClassName: ApiVisitUtil
 * @Description: api调用方法
 * @author fanyb
 * @date Aug 17, 2015 4:37:38 PM
 * @version v1.0
 */
public class ApiVisitUtil {
  private static final Logger LOG = LoggerFactory.getLogger(ApiVisitUtil.class);

  /**
   * 
   * @author fanyb
   * @Description: 调用服务器方法封装
   * @param URL 服务器提供的路径
   * @param method 提交的方法形式
   * @param map 传入的参数
   * @return String string对象
   * @throws ApiRequestException
   * @throws Exception
   * @date Aug 17, 20152:04:43 PM
   *
   * @修改人
   * @修改日期
   */
  public static String doServerMethod(String URL, String method, Map<String, Object> map)
      throws ApiRequestException {
    // 查询参数构造
    String content = paramConstruction(map);
    LOG.debug("doServerMethod--------------------:paramConstruction: {}", content);
    // 使用的url
    String url = urlConstruction(URL, method, content);
    LOG.debug("doServerMethod--------------------:url: {}", url);
    // URLConnection连接构造
    return connection(url, method, content);
  }

  /**
   * 
   * @author fanyb
   * @Description: 参数构造
   * @param map
   * @return String
   * @date Nov 28, 201510:20:31 AM
   *
   * @修改人
   * @修改日期
   */
  public static String paramConstruction(Map<String, Object> map) {
    StringBuffer sb = new StringBuffer();
    if (map != null) {
      for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getValue() != null) {
          try {
            sb.append(entry.getKey() + "=" + URLEncoder.encode(entry.getValue().toString(), "utf-8")
                + "&");
          } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            sb.append(entry.getKey() + "=" + entry.getValue().toString() + "&");
          }
        }
      }
      // 最后一个字符位置不能带&符号
      String result = sb.toString();
      return result.substring(0, result.lastIndexOf("&"));
    }
    return "";
  }

  /**
   * 
   * @author fanyb
   * @Description: 传入参数的构造
   * @param URL
   * @param method
   * @param content
   * @return String
   * @date Nov 28, 201510:20:51 AM
   *
   * @修改人
   * @修改日期
   */
  public static String urlConstruction(String URL, String method, String content) {
    // 如果提交方法是post,则url不带参数
    if (ApiConstants.POST.equals(method.toUpperCase())) {
      return URL + "?" + ApiConstants.TOKEN;
    } else {// 提交方法是get,带参数
      return URL + "?" + content + "&" + ApiConstants.TOKEN;
    }
  }

  /**
   * 
   * @author fanyb
   * @Description: 连接api
   * @param url
   * @param method
   * @param content
   * @return String
   * @throws ApiRequestException
   * @date Nov 28, 201510:44:57 AM
   *
   * @修改人
   * @修改日期
   */
  public static String connection(String url, String method, String content)
      throws ApiRequestException {
    HttpURLConnection httpURLConnection = getHttpURLConnetion(url, method, content);

    InputStream inputStream = null;
    InputStreamReader inputStreamReader = null;
    BufferedReader reader = null;
    StringBuffer resultBuffer = new StringBuffer();
    String tempLine = null;
    int responseCode = -1;
    try {
      responseCode = httpURLConnection.getResponseCode();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      throw new ApiRequestException("Connection Refused请查看api是否正常启动", e1);
    }
    if (responseCode != 200) {
      LOG.error("doServerMethod-----------------------HTTP Request is not success");
      throw new ApiRequestException(
          "HTTP Request is not success, Response code is " + responseCode);
    }
    try {
      inputStream = httpURLConnection.getInputStream();
      inputStreamReader = new InputStreamReader(inputStream, "utf-8");
      reader = new BufferedReader(inputStreamReader);
      while ((tempLine = reader.readLine()) != null) {
        resultBuffer.append(tempLine);
      }
    } catch (Exception e) {
      LOG.error("doServerMethod-----------------------" + e.toString());
      throw new ApiRequestException("读入流错误", e);
    } finally {
      IOUtils.closeQuietly(reader);
      IOUtils.closeQuietly(inputStreamReader);
      IOUtils.closeQuietly(inputStream);
    }
    return resultBuffer.toString();
  }

  /**
   * @author fanyb
   * @Description: 获取HttpURLConnetion
   * @param url
   * @param method
   * @return HttpURLConnection
   * @throws ApiRequestException
   * @date Nov 28, 201511:32:40 AM
   *
   * @修改人
   * @修改日期
   */
  private static HttpURLConnection getHttpURLConnetion(String url, String method, String content)
      throws ApiRequestException {
    URLConnection connection = null;
    URL localURL = null;
    try {
      localURL = new URL(url);
      connection = localURL.openConnection();
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
      throw new ApiRequestException("url创建失败.", e1);
    } catch (IOException e1) {
      e1.printStackTrace();
      throw new ApiRequestException("connection创建失败.", e1);
    }

    HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
    httpURLConnection.setConnectTimeout(30000);
    httpURLConnection.setReadTimeout(30000);
    httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
    httpURLConnection.setRequestProperty("contentType", "utf-8");
    httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    // post方法参数提交方式
    if (ApiConstants.POST.equals(method.toUpperCase())) {
      // Output to the connection. Default is
      // false, set to true because post
      // method must write something to the
      // connection
      // 设置是否向connection输出，因为这个是post请求，参数要放在
      // http正文内，因此需要设为true
      httpURLConnection.setDoOutput(true);
      // Read from the connection. Default is true.
      httpURLConnection.setDoInput(true);
      // Set the post method. Default is GET
      try {
        httpURLConnection.setRequestMethod(ApiConstants.POST);
      } catch (ProtocolException e) {
        e.printStackTrace();
        throw new ApiRequestException("protocolException创建失败.", e);
      }
      // Post cannot use caches
      // Post 请求不能使用缓存
      httpURLConnection.setUseCaches(false);
      DataOutputStream out = null;
      try {
        out = new DataOutputStream(httpURLConnection.getOutputStream());
        out.writeBytes(content);
        out.flush();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        IOUtils.closeQuietly(out);
      }
    }
    return httpURLConnection;
  }

  /**
   * 
   * @author fanyb
   * @Description: 将api返回的json转换成列表
   * @param jsonText api返回的json
   * @param cls 类名
   * @return List<Object>
   * @date Aug 17, 20154:47:08 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> str2List(String jsonText, Class cls) {
    if (jsonText == null || "".equals(jsonText)) {
      return null;
    }
    LOG.debug("jsonText {}", jsonText);
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      List<T> list = JsonUtil.toList(obj.getString("list"), cls);
      return list;
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: 实体转换成map格式的值
   * @param obj 实体对象
   * @return Map<String,Object> 实体属性map格式值
   * @date Aug 19, 20155:06:21 PM
   *
   * @修改人
   * @修改日期
   */
  public static Map<String, Object> objectToMap(Object obj) {
    Map<String, Object> map = new HashMap<String, Object>();
    // 得到类对象
    Class cls = (Class) obj.getClass();
    // 得到类中的所有属性集合
    Field[] ds = cls.getDeclaredFields();
    for (int i = 0; i < ds.length; i++) {
      Field f = ds[i];
      f.setAccessible(true); // 设置些属性是可以访问的
      // 获取属性的名字
      String name = ds[i].getName();
      // 将属性的首字符大写，方便构造get，set方法
      // name = name.substring(0, 1).toUpperCase() + name.substring(1);
      try {
        // 得到此属性的值
        Object val = f.get(obj);
        map.put(name, val);
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }
    return map;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取数据源列表
   * @param terminalId 终端编号
   * @return List<Object>
   * @throws ApiRequestException
   * @date Aug 18, 20155:07:28 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getDataSourceList(String terminalId) throws ApiRequestException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("terminalId", terminalId);
    String jsonText = doServerMethod(ApiConstants.URL_DATASOURCE_LIST, ApiConstants.GET, map);
    LOG.debug("jsonText {}", jsonText);
    List<T> list = str2List(jsonText, DataSource.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取用户云存储列表
   * @param customerId 客户编号
   * @return List<Object> 用户云存储列表
   * @throws ApiRequestException
   * @throws Exception
   * @date Aug 18, 20155:34:05 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getUserStorageCloudList(String customerId) throws ApiRequestException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    String jsonText = doServerMethod(ApiConstants.URL_SCLOUD_LIST, ApiConstants.GET, map);
    List<T> list = str2List(jsonText, StorageCloud.class);
    return list;
  }

  /**
   * @author 张建峰
   * @Description: 多条件查询backupLog
   * @param @param backupLog
   * @param @return
   * @param @throws Exception
   * @return List<T>
   * @throws ApiRequestException
   * @date 2015年9月23日上午9:21:40
   *
   * @修改人 张建峰
   * @修改日期
   */
  public static <T> List<T> getBackupLogList(BackupLog backupLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(backupLog);
    String jsonText = doServerMethod(ApiConstants.URL_BACKUPLOG_LIST, ApiConstants.GET, map);
    LOG.info("getBackupLogList {}", jsonText);
    List<T> list = str2List(jsonText, BackupLog.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取云存储的前端列表
   * @param storageCloudId 云编号
   * @return List<Object> 云存储的前端列表
   * @throws ApiRequestException
   * @throws Exception
   * @date Aug 18, 20156:07:20 PM
   *
   * @修改人
   * @修改日期
   */
  public static <T> List<T> getStorageCloudServerList(String storageCloudId)
      throws ApiRequestException {

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("storageCloudId", storageCloudId);
    String jsonText = doServerMethod(ApiConstants.URL_SSERVER_LIST, ApiConstants.GET, map);
    List<T> list = str2List(jsonText, StorageServer.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 添加文件变更日志
   * @param fileChangeLog 文件变更日志对象
   * @return String 是否成功
   * @throws ApiRequestException
   * @throws Exception
   * @date Aug 18, 20156:24:39 PM
   *
   * @修改人
   * @修改日期
   */
  public static String addFileChangeLog(FileChangeLog fileChangeLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(fileChangeLog);

    String jsonText = doServerMethod(ApiConstants.URL_FILE_CHANGE_LOG_ADD, ApiConstants.POST, map);
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  public static String addHeartLog(HeartLog heartLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(heartLog);

    String jsonText = doServerMethod(ApiConstants.URL_HEART_LOG_ADD, ApiConstants.GET, map);
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * @author 张建峰
   * @Description: 新增备份日志
   * @param @param backupLog
   * @param @return
   * @param @throws Exception
   * @return String
   * @throws ApiRequestException
   * @date 2015年9月23日上午8:31:36
   *
   * @修改人 张建峰
   * @修改日期
   */
  public static String addBackupLog(BackupLog backupLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(backupLog);

    String jsonText = doServerMethod(ApiConstants.URL_BACKUP_LOG_ADD, ApiConstants.POST, map);
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * @author 张建峰
   * @Description: 更新备份日志
   * @param @param backupLog
   * @param @return
   * @param @throws Exception
   * @return String
   * @throws ApiRequestException
   * @date 2015年9月23日上午9:04:18
   *
   * @修改人 张建峰
   * @修改日期
   */
  public static String updateBackupLog(BackupLog backupLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(backupLog);

    String jsonText = doServerMethod(ApiConstants.URL_BACKUP_LOG_UPDATA, ApiConstants.POST, map);
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据终端编号获取客户对象（没有调用）
   * @param terminalId 终端编号
   * @return Customer 客户对象
   * @throws ApiRequestException
   * @throws Exception
   * @date Aug 19, 201510:55:57 AM
   *
   * @修改人
   * @修改日期
   */
  public static Customer getCustomerByTerminalId(String terminalId) throws ApiRequestException {
    Customer customer = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("terminalId", terminalId);
    String jsonText =
        doServerMethod(ApiConstants.URL_GET_CUSTOMER_BY_TERMINALID, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return customer;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      customer = (Customer) JsonUtil.toObject(obj.get("object").toString(), Customer.class);
      return customer;
    }
    return customer;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新数据源
   * @param datasource 数据源
   * @return String 更新結果
   * @throws ApiRequestException
   * @date Aug 19, 20155:15:10 PM
   *
   * @修改人
   * @修改日期
   */
  public static boolean updateDataSourceImpl(Map<String, Object> map) {    
    boolean flag = false;
    String jsonText = null;
    try {
      jsonText = doServerMethod(ApiConstants.URL_UPDATE_DATASOURCE, ApiConstants.POST, map);
    } catch (ApiRequestException e) {
      e.printStackTrace();
    }
    if (jsonText != null && !"".equals(jsonText)) {
      JSONObject obj = JSONObject.parseObject(jsonText);
      if ("200".equals(obj.get("code").toString())) {
        flag = true;
      }
    }

    return flag;
  }
  
  public static String updataDatasource(DataSource datasource) {
    Map<String, Object> map = objectToMap(datasource);
    if(updateDataSourceImpl(map) == false){
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      updateDataSourceImpl(map);
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 获取数据源对象
   * @param id 数据源编号
   * @return DataSource 数据源对象
   * @throws ApiRequestException
   * @date Aug 20, 201511:40:58 AM
   *
   * @修改人
   * @修改日期
   */
  public static DataSource getDataSource(String id) throws ApiRequestException {
    DataSource datasource = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("id", id);
    String jsonText = doServerMethod(ApiConstants.URL_GET_DATASOURCE, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return datasource;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      datasource = (DataSource) JsonUtil.toObject(obj.getString("obj"), DataSource.class);
      return datasource;
    }
    return datasource;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据数据源编号获取备份策略
   * @param datasourceId 数据源编号
   * @return BackStrategy 备份策略
   * @throws ApiRequestException
   * @date Aug 24, 20155:58:55 PM
   *
   * @修改人
   * @修改日期
   */
  public static BackStrategy getBackStrategyByDatasourceId(String datasourceId)
      throws ApiRequestException {
    BackStrategy backStrategy = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("datasourceId", datasourceId);
    String jsonText = doServerMethod(ApiConstants.URL_GET_BACKSTRATEGY, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return backStrategy;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      backStrategy = (BackStrategy) JsonUtil.toObject(obj.getString("object"), BackStrategy.class);
      return backStrategy;
    }
    return backStrategy;
  }

  /**
   * 
   * @author fanyb
   * @Description: 更新备份策略
   * @param backStrategy 备份策略对象
   * @return String 更新结果
   * @throws ApiRequestException
   * @date Aug 27, 201511:02:22 AM
   *
   * @修改人
   * @修改日期
   */
  public static String updataBackStrategy(BackStrategy backStrategy) throws ApiRequestException {
    Map<String, Object> map = objectToMap(backStrategy);
    String jsonText = doServerMethod(ApiConstants.URL_UPDATE_BACK_STRATEGY, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return "false";
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  public static String deleteBackupLog(BackupLog backupLog) throws ApiRequestException {
    Map<String, Object> map = objectToMap(backupLog);
    String jsonText = doServerMethod(ApiConstants.URL_BACKUP_LOG_DELETE, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return "false";
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  public static String deleteFileChangeLog(String dsId) throws ApiRequestException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("dsId", dsId);
    String jsonText = doServerMethod(ApiConstants.URL_FILECHANGE_LOG_DELETE, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return "false";
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      return "success";
    }
    return "false";
  }

  /**
   * 
   * @author fanyb
   * @Description: 判断存储文件是否存在
   * @param datasourceId 数据源编号
   * @param signature 文件签名
   * @return String
   * @throws ApiRequestException
   * @date Sep 6, 20159:23:42 AM
   *
   * @修改人
   * @修改日期
   */
  public static boolean storageFileIsExist(String datasourceId, String signature)
      throws ApiRequestException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("datasourceId", datasourceId);
    map.put("signature", signature);
    String jsonText =
        doServerMethod(ApiConstants.URL_GET_STORAGE_FILE_BY_DSID_SIGN, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return false;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString())) {
      StorageFile file =
          (StorageFile) JsonUtil.toObject(obj.getString("object"), StorageFile.class);
      if (file != null) {
        return true;
      }
    }
    return false;
  }

  public static <T> List<T> getTbStorageStrategyByCustomerId(String customerId)
      throws ApiRequestException {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    LOG.info("getTbStorageStrategyByCustomerId--------------------:customerId:" + customerId);
    String jsonText =
        doServerMethod(ApiConstants.URL_TB_STORAGE_STRATEGY_BY_CUSTOMER_ID, ApiConstants.GET, map);
    LOG.info("getTbStorageStrategyByCustomerId--------------------:jsonText:" + jsonText);
    List<T> list = str2List(jsonText, TbStorageStrategy.class);
    return list;
  }

  /**
   * 
   * @author fanyb
   * @Description: 根据客户编号查找customerkey
   * @param customerId
   * @return String
   * @throws ApiRequestException
   * @date Oct 24, 20158:55:31 AM
   *
   * @修改人
   * @修改日期
   */
  public static String findCustomerKeyByCustomerId(String customerId) throws ApiRequestException {
    Customer customer = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    String jsonText = doServerMethod(ApiConstants.URL_FIND_CUSTOMER_KEY, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return null;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      customer = (Customer) JsonUtil.toObject(obj.getString("obj"), Customer.class);
      return customer.getCustomerKey();
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: 查找客户加密key
   * @param customerId
   * @return String
   * @throws ApiRequestException
   * @date Oct 24, 20159:17:27 AM
   *
   * @修改人
   * @修改日期
   */
  public static String findCustomerEncryptionKeyByCustomerId(String customerId)
      throws ApiRequestException {
    CustomerEncryption customerEncryption = null;
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("customerId", customerId);
    String jsonText = doServerMethod(ApiConstants.URL_CUSTOMER_ENCRYPTION, ApiConstants.GET, map);
    if (jsonText == null || "".equals(jsonText)) {
      return null;
    }
    JSONObject obj = JSONObject.parseObject(jsonText);
    if ("200".equals(obj.get("code").toString()) && obj.getString("obj") != null) {
      customerEncryption =
          (CustomerEncryption) JsonUtil.toObject(obj.getString("obj"), CustomerEncryption.class);
      return customerEncryption.getEncryptionKey();
    }
    return null;
  }
}
